package com.ruoyi.web.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.mapper.MesBindingDailyMapper;
import com.ruoyi.web.vo.MesBindingDaily;
import com.ruoyi.web.vo.ProducePiece;
import com.ruoyi.web.vo.ProducePrint;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class MesBindingDailyService extends ServiceImpl<MesBindingDailyMapper, MesBindingDaily> {
    @Resource
    private ProducePrintService producePrintService;
    @Resource
    private ProducePieceService producePieceService;

    /**
     * 根据生产单和产品查询贴集合
     *
     * @param produceId      生产单id
     * @param orderProductId 产品id
     * @param strList        排除状态 整代 半代
     * @return 当前步骤累计报工量，上一步累计报工量，当前步骤，上一步骤
     */
    public TwoValues getPieceList(String produceId, String orderProductId, List<String> strList) {
        String returnMsg = "";
        List<ProducePrint> printList = producePrintService.list(new QueryWrapper<ProducePrint>().lambda()
                .eq(ProducePrint::getProduceId, produceId)
                .eq(ProducePrint::getOrderProductId, orderProductId)
                .and(it -> it.notIn(ProducePrint::getStatus, strList).or().isNull(ProducePrint::getStatus))
        );
        if (printList.size() == 0) {
            returnMsg = "没有找到对应的生产印刷单";
            // return AjaxResult.error("没有找到对应的生产印刷单");
        }
        List<ProducePiece> pieceList =new ArrayList<>();
        if (StringUtil.isEmpty(returnMsg)) {
            List<String> ids = printList.stream().map(it -> it.getPrintId()).collect(Collectors.toList());
            pieceList = producePieceService.list(new QueryWrapper<ProducePiece>().lambda()
                    .in(ProducePiece::getPrintId, ids));
            if (pieceList.size() == 0) {
                returnMsg = "生产印刷单没有贴的信息";
                // return AjaxResult.error("生产印刷单没有贴的信息");
            }
        }

        new TwoValues(returnMsg, pieceList);
        return new TwoValues(returnMsg, pieceList);
    }





}

